/**
 * Created by WuHao on 2017/1/31.
 */

var express = require('express');
var User = require('../models/user');

var router = express.Router();

/**
 * 定义统一的返回格式
 */
var responseData;

/**
 * 初始化返回数据
 */
router.use(function(req, res, next){
    responseData = {
        code: 0,
        message: ''
    };

    next();
});

/**
 * 用户注册
 *  1. 用户名、密码、第二次密码不能为空
 *  2. 密码和第二次密码必须一致
 *  3. 用户是否已经被注册
 *     查询数据库中的数据
 *          存在->提示
 *          不存在->注册成功->跳转页面
 *
 */
router.post('/user/register', function(req, res, next){
    var param = req.body;

    var username = param.username;
    var password = param.password;
    var repassword = param.repassword;

    if(username == ''){
        responseData.code = 1;
        responseData.message = '用户名不能为空';
        res.json(responseData);
        return;
    }

    if(password == ''){
        responseData.code = 2;
        responseData.message = '密码不能为空';
        res.json(responseData);
        return;
    }

    if(repassword == ''){
        responseData.code = 3;
        responseData.message = '确认密码不能为空';
        res.json(responseData);
        return;
    }

    if(password != repassword){
        responseData.code = 4;
        responseData.message = '两次输入的密码必须一致';
        res.json(responseData);
        return;
    }

    User.findOne({
        username: username
    }).then(function(userInfo){
        if(userInfo){
            responseData.code = 5;
            responseData.message = '该用户名已注册';
            res.json(responseData);
            return;
        }

        var user = new User({
            username: username,
            password: password
        });

        return user.save();

    }).then(function(newUserInfo){

        req.cookie.set('userInfo', JSON.stringify({
            _id: newUserInfo._id,
            username: newUserInfo.username
        }));

        responseData.message = '注册成功';
        res.json(responseData);
    });

});

/**
 * 用户登陆
 *   1.用户名密码不能为空
 *   2.用户名密码必须和数据库中一致
 *      成功->保存cookie->提示->页面刷新
 *      失败->提示
 *
 */
router.post('/user/login', function(req, res, next){
    var param = req.body;

    var username = param.username;
    var password = param.password;

    if(username == ''){
        responseData.code = 1;
        responseData.message = '请输入用户名';
        res.json(responseData);
        return;
    }

    if(password == ''){
        responseData.code = 2;
        responseData.message = '请输入密码';
        res.json(responseData);
        return;
    }

    User.findOne({
        username: username,
        password: password
    }).then(function(userInfo){
        if(!userInfo){
            responseData.code = 6;
            responseData.message = '用户名或密码错误';
            res.json(responseData);
            return;
        }

        responseData.message = '登录成功';
        responseData.userInfo = {
            _id: userInfo._id,
            username: userInfo.username
        };

        // 登陆成功还需要保持数据到cookie中
        req.cookie.set('userInfo', JSON.stringify(responseData.userInfo));

        res.json(responseData);
    });

});

/**
 * 用户退出
 *   清除cookie中的内容
 */
router.get('/user/logout', function(req, res, next){
    req.cookie.set('userInfo', null);
    res.json(responseData);
});

module.exports = router;